package com.ruoyi.project.manage.approveLog.controller;

import java.util.List;

import com.ruoyi.common.constant.ProjectConstants;
import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.project.manage.project.domain.Project;
import com.ruoyi.project.manage.project.service.IProjectService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.manage.approveLog.domain.ApproveLog;
import com.ruoyi.project.manage.approveLog.service.IApproveLogService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 审批流转记录Controller
 * 
 * @author fangkun
 * @date 2022-03-28
 */
@Controller
@RequestMapping("/manage/approveLog")
public class ApproveLogController extends BaseController
{
    private String prefix = "manage/approveLog";

    @Autowired
    private IApproveLogService approveLogService;
    @Autowired
    private IProjectService projectService;

    @RequiresPermissions("manage:approveLog:view")
    @GetMapping()
    public String approveLog()
    {
        return prefix + "/projectLog";
    }

    /**
     * 查询审批流转记录列表
     */
    @RequiresPermissions("manage:approveLog:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Project project)
    {
        startPage();
        project.setProjectStatus(ProjectConstants.APPROVAL_AGREE);
        // 设置当前登录人查看本人录入的项目信息
        project.setCreateBy(ShiroUtils.getLoginName());
        List<Project> list = projectService.selectProjectList(project);
        return getDataTable(list);
    }

    @GetMapping("/addLog/{projectId}")
    public String addLog(@PathVariable("projectId") Long projectId, ModelMap mmap)
    {
        Project project = projectService.selectProjectByProjectId(projectId);
        mmap.put("project", project);
        return prefix + "/add";
    }

    @GetMapping("/showLog/{projectId}")
    public String showLog(@PathVariable("projectId") Long projectId, ModelMap mmap)
    {
        Project project = projectService.selectProjectByProjectId(projectId);
        mmap.put("project", project);

        return prefix + "/showLog";
    }

    @PostMapping("/searchLog/{projectId}/{period}")
    @ResponseBody
    public TableDataInfo searchLog(@PathVariable("projectId") Long projectId, @PathVariable("period") String period, ModelMap mmap)
    {
        startPage();
        // 查询项目日志，包含每日跟进和项目事项日志
        List<ApproveLog> approveLogs = approveLogService.selectProjectLogList(projectId, period);
        return getDataTable(approveLogs);
    }

    /**
     * 导出审批流转记录列表
     */
    @RequiresPermissions("manage:approveLog:export")
    @Log(title = "审批流转记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ApproveLog approveLog)
    {
        List<ApproveLog> list = approveLogService.selectApproveLogList(approveLog);
        ExcelUtil<ApproveLog> util = new ExcelUtil<ApproveLog>(ApproveLog.class);
        return util.exportExcel(list, "审批流转记录数据");
    }

    /**
     * 新增审批流转记录
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存审批流转记录
     */
    @Log(title = "审批流转记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ApproveLog approveLog)
    {
        return toAjax(approveLogService.insertApproveLog(approveLog));
    }

    /**
     * 修改审批流转记录
     */
    @RequiresPermissions("manage:approveLog:edit")
    @GetMapping("/edit/{approveLogId}")
    public String edit(@PathVariable("approveLogId") Long approveLogId, ModelMap mmap)
    {
        ApproveLog approveLog = approveLogService.selectApproveLogByApproveLogId(approveLogId);
        mmap.put("approveLog", approveLog);
        return prefix + "/edit";
    }

    /**
     * 修改保存审批流转记录
     */
    @RequiresPermissions("manage:approveLog:edit")
    @Log(title = "审批流转记录", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ApproveLog approveLog)
    {
        return toAjax(approveLogService.updateApproveLog(approveLog));
    }

    /**
     * 删除审批流转记录
     */
    @RequiresPermissions("manage:approveLog:remove")
    @Log(title = "审批流转记录", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(approveLogService.deleteApproveLogByApproveLogIds(ids));
    }
}
